<?php

namespace app\common\model\publics;

use app\common\model\User;
use think\Model;
use think\Db;

class Likecoll extends Model
{
	// 设置当前模型使用的完整数据库连接信息
	protected $connection = 'database.db_second';//连接配置
    // 表名
    protected $name = 'publics_likecoll';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
	    'createtime_text',
	    'avatar',
	    'nickname',
        'type_text'
    ];

	public function getNicknameAttr($value, $data){

		$value = Db::name("user")->where(['id'=>$data['user_id']])->value('nickname');

		return $value ? $value : '';

	}


	public function getAvatarAttr($value, $data){

		$value = Db::name("user")->where(['id'=>$data['user_id']])->value('avatar');

		return $value ? $value : '';

	}
    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


	public function trends()
	{
		return $this->belongsTo('app\common\model\Trends', 'trends_id', 'id', [], 'INNER')->setEagerlyType(0);
	}



	//获取用户信息
	public function getuser($trends,$user_id){

		$user = User::get($trends->user_id);

		$data = [];
		$data['nickname'] = $trends->is_name == 1?$user->anony:$user->nickname;
		$data['avatar'] = $user->avatar;
		//显示级别
		$circle = Db::connect($this->connection)->name('circle_user')->where(['user_id'=>$trends->user_id,'circle_id'=>$trends->circle_id])->find();

		if($circle){
			$data['level'] = $circle['level'];
			$level = Db::connect($this->connection)->name('publics_level')->where(['weigh'=>$circle['level']])->find();
			$nickname = Db::connect($this->connection)->name('circle_level')->where(['level'=>$level['weigh'],'circle_id'=>$trends->circle_id])->value('nickname');
			$data['level_name'] = $nickname?$nickname:$level['name'];
			$data['icon'] = cdnurl($level['image'],true);
			$data['bg_color'] = $level['bg_color'];
			$data['font_color'] = $level['font_color'];
			if($trends->user_id == $user_id && $circle['identity'] < 3){

				$data['identity'] = $circle['identity'];
			}else{
				$data['identity'] = 0;
			}

		}else{

			$circle = Db::connect($this->connection)->name('circle_user')->where(['user_id'=>$trends->user_id,'circle_id'=>1])->find();
			$data['level'] = $circle['level'];
			$level = Db::connect($this->connection)->name('publics_level')->where(['weigh'=>$circle['level']])->find();
			$data['level_name'] = $level['name'];
			$data['icon'] = cdnurl($level['image'],true);
			$data['bg_color'] = $level['bg_color'];
			$data['font_color'] = $level['font_color'];
			$data['identity'] = 0;
		}

		return $data;
	}


	public function getCreateTimeTextAttr($value, $data)
	{
		$value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
		return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
	}

}
